package des.wangku.operate.task.news.bookssearch;

import static des.wangku.operate.standard.utls.UtilsJsoupConst.ACC_JsoupRulePrecisePositioningOutPage;
import static des.wangku.operate.standard.utls.UtilsConsts.ACC_ENTER;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import des.wangku.operate.standard.testing.Books17Utils;
import des.wangku.operate.standard.testing.Books17Utils.StyleClass;
import des.wangku.operate.standard.utls.UtilsCollect;
import des.wangku.operate.standard.utls.UtilsFile;
import des.wangku.operate.standard.utls.UtilsHtmlFilter;
import des.wangku.operate.standard.utls.UtilsJsoup;
import des.wangku.operate.standard.utls.UtilsJsoupLink;
import des.wangku.operate.standard.utls.UtilsList;
import des.wangku.operate.standard.utls.UtilsReadURL;
import des.wangku.operate.standard.utls.UtilsThread;
import des.wangku.operate.standard.utls.UtilsCollect.CollectGroup;
import des.wangku.operate.standard.utls.UtilsThread.UtilsInterfaceThreadUnit;

public class Utils {

	public static final void makelist(int maxThread, List<UnitFirstClass> newlist) {
		List<CollectGroup> list=newlist.stream()
			    .filter(obj -> obj instanceof CollectGroup)
			    .map(obj -> (CollectGroup) obj)
			    .collect(Collectors.toList());
		List<List<CollectGroup>> list2 = UtilsCollect.groupAverage(list, maxThread, false);
		UtilsCollect.showList(list2);
		List<List<UtilsInterfaceThreadUnit>> workList2 = new ArrayList<>(list2.size());
		for (List<CollectGroup> list3 : list2) {
			List<UtilsInterfaceThreadUnit> newlist2 = new ArrayList<>();
			for (CollectGroup f : list3) {
				UnitFirstClass g = (UnitFirstClass) f;
				ReadWorkOne t = new ReadWorkOne(g);
				newlist2.add(t);
			}
			workList2.add(newlist2);
		}

		UtilsThread.startThreadWork(workList2);
	}

	static final class UnitFirstClass implements CollectGroup {
		String url;
		String filename;
		String contentkey=null;
		String downfiletag=null;
		String targettag=null;
		String itemtag=null;
		boolean autotitle=false;
		String urlbad=null;
		Elements lis = new Elements();

		public UnitFirstClass(String filename,String url) {
			this.filename = filename;
			this.url = url;
		}
		public void init() {
			if(downfiletag!=null && downfiletag.length()>0) return;
			/*
			Document doc = UtilsJsoup.getDoc(this.url);
			if (doc == null) {
				System.out.println("doc:null\t" + url);
				return;
			}
			if (isexist(filename)) return; 
			Element f = UtilsJsoup.getElementFirst(doc, e.keyLink);
			if (f == null) {
				System.out.println("f:null\t" + url);
				return;
			}
			lis = f.select("a");
			*/
			/* 没有下载文件，只能从li提取各个章节 */
			
			
		}
		@Override
		public int number() {
			return lis.size();
		}

		@Override
		public String toString() {
			return "[" + filename + "/" + url + "]";
		}

	}

	static final class ReadWorkOne implements UtilsInterfaceThreadUnit {
		UnitFirstClass uc = null;


		public ReadWorkOne(UnitFirstClass uc) {
			this.uc = uc;
		}

		@Override
		public void run() {
			//Document doc = UtilsJsoup.getDoc(uc.url);
			if (isexist(uc.filename)) return;
			String pathfile = Consts.ACC_Path + uc.filename + ".txt";
			System.out.println("pathfile:"+pathfile);
			/* 有文件可以下载 */
			if(uc.downfiletag!=null && uc.downfiletag.length()>0) {
				String downfiletag =uc.downfiletag;
				//Elements es=UtilsJsoup.getAllElementsByKey(uc.url, downfiletag);
				//if(es.size()==0)return;
				//Element first=es.first();
				String fileurl=UtilsJsoupLink.getHrefFirst(uc.url, downfiletag);
				if(fileurl==null)return;
				/* 有下载文件，需要提取此链接 */
				UtilsReadURL.downfile(fileurl, uc.filename + ".txt", Consts.ACC_Path);
				File file = new File(pathfile);
				movefile(file);
				return;
			}
			String itemurl=uc.url;
			if(uc.targettag!=null && uc.targettag.length()>0) {
				String itemurl2=null;
				if(uc.targettag.indexOf(ACC_JsoupRulePrecisePositioningOutPage) >-1 ) {
					itemurl2=UtilsJsoupLink.getRelationHrefFirst(uc.url,uc.targettag);
					
				}else {
					itemurl2= UtilsJsoupLink.getHrefFirst(uc.url,uc.targettag);
				}
				if(itemurl2!=null)itemurl=itemurl2;
			}
			if(uc.itemtag!=null && uc.itemtag.length()>0) {
				UtilsThread.ThreadSleep(2000);
				Elements es=UtilsJsoup.getElementAll(itemurl, uc.itemtag);
				System.out.println("es:"+es.size());
				File file = new File(pathfile);
				int index=0;
				es=UtilsJsoupLink.removeNoHref(es);
				es=UtilsList.distinctText(es);
				//for(Element ee:es) {
				//	//System.out.println(uc.itemtag+":::"+UtilsJsoupLink.getHrefFirst(ee));
				//}
				//String[] badArray=uc.urlbad.split(",");
				for(int i=0,len=es.size();i<len;i++) {
					Element li = es.get(i);
					String name = "";
					String href=UtilsJsoupLink.getHrefFirst(li);
					String content = "";
					if(href==null)continue;
					//for(String ee:badArray)
						//if(href.indexOf(ee)>-1)continue loop;
					index++;
					name = li.text();
					name=UtilsHtmlFilter.filterNumberHtmlSymbol(name);
					String newname = getnewLine(name,index);
					if(uc.autotitle) {
						String line1 = newname + ACC_ENTER;
						UtilsFile.writeFile(file,line1);
					}
					Element condiv = UtilsJsoup.getElementFirst(href, uc.contentkey);
					if (condiv == null) continue;
					content = condiv.html();
					content=UtilsHtmlFilter.filterNumberHtmlSymbol(content);
					content = UtilsHtmlFilter.filterNumberHtmlSymbol(content);
					content = UtilsHtmlFilter.filter(content);
					UtilsFile.writeFile(file, content+ACC_ENTER);
					System.out.println(itemurl + " -> " + uc.filename + "[" + index + "/" + len + "] -> " + newname);
					
				}
			}
		}

	}
	public static void main(String[] args) 
	{
		String head="https://www.yunyue.cc";
		String filesource="g:/abc.txt";
		String name="擎天战记";
		String contentkey="content";
		String pathfile = Consts.ACC_Path + name+ ".txt";
		File file = new File(pathfile);
		String content=UtilsFile.readFile(filesource).toString();
		Document doc=Jsoup.parse(content, head);
		Elements es=doc.select("a[href]");
		for(int i=0;i<es.size();i++) {
			int index=i+1;
			Element e=es.get(i);
			String itemname=e.text();
			String itemurl=e.attr("abs:href");
			String newname = getnewLine(itemname,index);
			String line1 = ACC_LineSize + newname + ACC_LineSize;
			UtilsFile.writeFile(file,line1);

			Element condiv = UtilsJsoup.getElementFirst(itemurl, contentkey);
			if (condiv == null) continue;
			//UtilsJsoup.remove(condiv, uc.contentkey);
			content = condiv.html();
			content = UtilsHtmlFilter.filter(content);
			

			UtilsFile.writeFile(file,content);
			
		}
	}
	static final String getnewLine(String name,int index) {
		if(name==null)return "";
		name=name.trim();
		if(name.indexOf("第")>-1)return name;
		return "第" + index + "章 " + name;
	}
	static final String ACC_LineSize=ACC_ENTER + ACC_ENTER;

	static final String getFilename(String keyNameRemove, String filename) {
		if (keyNameRemove != null && keyNameRemove.trim().length() > 0) {
			String remove = keyNameRemove.trim();
			String[] arr = remove.split(",");
			for (String ee : arr)
				filename = filename.replaceAll(ee, "");

		}
		return filename;
	}

	static final boolean isexist(String filename) {
		File file = UtilsFile.getFilePath(Consts.ACC_SavePath, filename + ".txt");
		return file != null;
	}

	public static void movefile(File file) {
		if(file==null || !file.exists())return;
		 long length = file.length();
		 StyleClass sc = Books17Utils.getSC(length);
		 String filename=file.getName();
		 String to = Consts.ACC_SavePath + sc.key + "/" + filename;
		 File endFile = new File(to);
		 file.renameTo(endFile);
		 System.out.println("移动到 -> " + to);
		
	}
}
